﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Configuration;
using System.Data.SqlClient;
using Website.Logger;
using DAL;
using UtilityExtensions;
using BOL;
using BOL.UI;
using WebLib;
using System.Web.Script.Services;
using System.Web.Services;

namespace UILGestion.Seguridad.Empleado
{
    public partial class EmpleadoProyectoEditGrid : System.Web.UI.UserControl
    {
        public EmpleadoProyectoEditGrid()
        {

            _db = new dbGestionDataContext(Parametros.strConnString);
            bol= new BOL.EmpleadoBL();
        }
        private dbGestionDataContext _db;
        private BOL.EmpleadoBL bol;

       private void setFooterRowObras()
        {


            DropDownList obraInsertDropDownList = ((DropDownList)itemGridView.FooterRow.FindControl("proyectoInsertDropDownList"));

              int idempleado=idEmpleado.Value.ToInt();
            var q = from a in DbUtil.Db.EmpleadoProys
                    where a.idEmpleado==idempleado
                    
                    select a.idProyecto;

            var qw = from b in DbUtil.Db.Proyectos
                      
                     where !q.Contains(b.idInternal)
                      & b.idProyectoTipo == 50
                     select b;

            obraInsertDropDownList.DataValueField = "idInternal";
            obraInsertDropDownList.DataTextField = "descripcion";
            obraInsertDropDownList.DataSource = qw.OrderBy(p=>p.descripcion).ToList();
            obraInsertDropDownList.DataBind();



            DropDownList rolInsertDropDownList = ((DropDownList)itemGridView.FooterRow.FindControl("rolInsertDropDownList"));

            rolInsertDropDownList.DataValueField = "idInternal";
            rolInsertDropDownList.DataTextField = "descripcion";
            rolInsertDropDownList.DataSource = DbUtil.Db.Rols.ToList();
            rolInsertDropDownList.DataBind();

        }

       private void setEditRowObras(int i)
          {

              //int idempleado = idEmpleado.Value.ToInt();
              //var q = from a in DbUtil.Db.EmpleadoProys
              //        where a.idEmpleado == idempleado
              //        select a.idProyecto;

              //var qw = from b in DbUtil.Db.Proyectos

              //         where !q.Contains(b.idInternal)
              //         select b;

              //DropDownList obraDropDownList = ((DropDownList)itemGridView.Rows[i].FindControl("obraInsertDropDownList"));
              //TextBox idobratxt = ((TextBox)itemGridView.Rows[i].FindControl("idObratxt"));


              //obraDropDownList.DataValueField = "idInternal";
              //obraDropDownList.DataTextField = "descripcion";
              //obraDropDownList.DataSource = qw;
              //obraDropDownList.DataBind();
              //obraDropDownList.SelectedValue = idobratxt.Text;

              DropDownList rolDropDownList = ((DropDownList)itemGridView.Rows[i].FindControl("rolEditDropDownList"));
              TextBox idRoltxt = ((TextBox)itemGridView.Rows[i].FindControl("idRoltxt"));


              rolDropDownList.DataValueField = "idInternal";
              rolDropDownList.DataTextField = "descripcion";
              rolDropDownList.DataSource = DbUtil.Db.Rols; 
              rolDropDownList.DataBind();
              rolDropDownList.SelectedValue = idRoltxt.Text;


          }
         
        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                if (!this.IsPostBack)
                {

                
                }
            
            }
            catch (SqlException sqlex)
            {
                WebServerSideSqlError.Log(sqlex);

                ScriptManager.RegisterClientScriptBlock(this, typeof(Page),
                 "uniqueKey" + DateTime.Now, "alert('" + sqlex.Message.Replace("'", "") + "');", true);


            }
            catch (Exception ex)
            {
                WebServerSideError.Log(ex);
                ScriptManager.RegisterClientScriptBlock(this, typeof(Page),
                     "uniqueKey" + DateTime.Now, "alert('" + ex.Message.Replace("'", "") + "');", true);

            }
            
            
        }
       
        
        public void Load(int idEmpleado)
        {
            this.idEmpleado.Value = idEmpleado.ToString();

            BindDataItem(); 
        }


        protected void AddItem(object sender, EventArgs e)
        {
            try
            {


                DropDownList proyectoDDL =
                 ((DropDownList)itemGridView.FooterRow.FindControl("ProyectoInsertDropDownList"));

                DropDownList rolDDL =
               ((DropDownList)itemGridView.FooterRow.FindControl("rolInsertDropDownList"));

                bol.AddEmpleadoProyecto(_db, idEmpleado.Value.ToInt(), 
                                        proyectoDDL.SelectedValue.ToInt(), 
                                        rolDDL.SelectedValue.ToInt());

                BindDataItem();
                GridUpdatePanel.Update();
            }
            catch (SqlException sqlex)
            {
                WebServerSideSqlError.Log(sqlex);
                ScriptManager.RegisterClientScriptBlock(this, typeof(UserControl),
                 "uniqueKey" + DateTime.Now, "alert('" + sqlex.Message.Replace("'", "").Replace("\r", "").Replace("\n", "") + "');", true);


            }
            catch (Exception ex)
            {
                WebServerSideError.Log(ex);
                ScriptManager.RegisterClientScriptBlock(this, typeof(UserControl),
                 "uniqueKey" + DateTime.Now, "alert('" + ex.Message.Replace("'", "") + "');", true);

            }

        }
        protected void UpdateItem(object sender, GridViewUpdateEventArgs e)
        {
            try
            {
                GridView gv = ((GridView)sender);
                int idinternal = int.Parse(gv.DataKeys[e.RowIndex].Values[0].ToString());

                //DropDownList obra =
                //    ((DropDownList)gv.Rows[e.RowIndex].FindControl("proyectoEditDropDownList"));

                DropDownList rol =
                    ((DropDownList)gv.Rows[e.RowIndex].FindControl("rolEditDropDownList"));

                bol.UpdateEmpleadoProyecto(_db, idinternal, rol.SelectedValue.ToInt());

                gv.EditIndex = -1;
                BindDataItem();

            }
            catch (SqlException sqlex)
            {
                WebServerSideSqlError.Log(sqlex);
                ScriptManager.RegisterClientScriptBlock(this, typeof(UserControl),
                 "uniqueKey" + DateTime.Now, "alert('" + sqlex.Message.Replace("'", "").Replace("\r", "").Replace("\n", "") + "');", true);


            }
            catch (Exception ex)
            {
                WebServerSideError.Log(ex);
                ScriptManager.RegisterClientScriptBlock(this, typeof(UserControl),
                 "uniqueKey" + DateTime.Now, "alert('" + ex.Message.Replace("'", "") + "');", true);

            }

        }
        protected void EditItem(object sender, GridViewEditEventArgs e)
        {
            try
            {
                itemGridView.EditIndex = e.NewEditIndex;

                BindDataItem();
                setEditRowObras(e.NewEditIndex);
                itemGridView.FooterRow.Visible = false;

            }
            catch (SqlException sqlex)
            {
                WebServerSideSqlError.Log(sqlex);
                ScriptManager.RegisterClientScriptBlock(this, typeof(UserControl),
                 "uniqueKey" + DateTime.Now, "alert('" + sqlex.Message.Replace("'", "").Replace("\r", "").Replace("\n", "") + "');", true);


            }
            catch (Exception ex)
            {
                WebServerSideError.Log(ex);
                ScriptManager.RegisterClientScriptBlock(this, typeof(UserControl),
                 "uniqueKey" + DateTime.Now, "alert('" + ex.Message.Replace("'", "") + "');", true);

            }

        }
        protected void CancelItem(object sender, GridViewCancelEditEventArgs e)
        {
            try
            {
                itemGridView.EditIndex = -1;
                BindDataItem();


            }
            catch (SqlException sqlex)
            {
                WebServerSideSqlError.Log(sqlex);
                ScriptManager.RegisterClientScriptBlock(this, typeof(UserControl),
                 "uniqueKey" + DateTime.Now, "alert('" + sqlex.Message.Replace("'", "").Replace("\r", "").Replace("\n", "") + "');", true);


            }
            catch (Exception ex)
            {
                ScriptManager.RegisterClientScriptBlock(this, typeof(UserControl),
                "uniqueKey" + DateTime.Now, "alert('" + ex.Message.Replace("'", "") + "');", true);

            }

        }
        protected void DeleteItem(object sender, EventArgs e)
        {
            try
            {

                ImageButton lnkRemove = (ImageButton)sender;
                int idInternal = int.Parse(lnkRemove.CommandArgument.ToString());


                bol.DeleteEmpleadoProyecto(_db,idInternal);
               
                BindDataItem();
              

            }
            catch (SqlException sqlex)
            {
                WebServerSideSqlError.Log(sqlex);
                ScriptManager.RegisterClientScriptBlock(this.Page, typeof(UserControl),
                 "uniqueKey" + DateTime.Now, "alert('" + sqlex.Message.Replace("'", "").Replace("\r", "").Replace("\n", "") + "');", true);


            }
            catch (Exception ex)
            {
                WebServerSideError.Log(ex);
                ScriptManager.RegisterClientScriptBlock(this, typeof(UserControl),
                 "uniqueKey" + DateTime.Now, "alert('" + ex.Message.Replace("'", "") + "');", true);

            }

        }

        private void resetEmpty()
        {
            if (itemGridView.Rows.Count == 0)
            {
                List<EmpleadoObraItemInfo> itemList0 = new List<EmpleadoObraItemInfo>();
                EmpleadoObraItemInfo itemcero = new EmpleadoObraItemInfo();
                
                itemcero.idInternal = 0;
                
                itemList0.Add(itemcero);
                itemGridView.DataSource = itemList0;
                itemGridView.DataBind();
                itemGridView.Rows[0].Visible = false;
            }

        }

        public void BindDataItem()
        {

            var empleadoObra = from a in DbUtil.Db.EmpleadoProys
                               where a.idEmpleado == idEmpleado.Value.ToInt()
                               & a.Proyecto.idProyectoTipo==50
                               select new EmpleadoObraItemInfo
                               {
                                   obraStr = a.Proyecto.descripcion,
                                   rolstr = a.Rol.descripcion,
                                   idInternal = a.idinternal,
                                   idRol = a.idRol,
                                   idObra = a.idProyecto
                                };

            itemGridView.DataSource = empleadoObra.OrderBy(p => p.idInternal); ;
            itemGridView.DataBind();
            if (itemGridView.Rows.Count>0)
            itemGridView.FooterRow.Visible = false;
            //resetEmpty();

        }

        protected void addObra_Click(object sender, EventArgs e)
        {
              //BindDataItem();
             
              resetEmpty();
              itemGridView.FooterRow.Visible = true;
            setFooterRowObras();
        }

    }

}